﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Text;

namespace HRM.Web.master
{
    public partial class ManageRoles : System.Web.UI.Page
    {
        #region Page load
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                BindGrid();
            }
            //var checkString = Request.Form["cbSelect"];

            //if (checkString == null)

            //    return;

            //// we'll need a split to get the individual ids

            //var values = checkString.Split(',');
        }
        #endregion

        #region Button Clicks
        protected void DeleteButton_Click(object sender, EventArgs e)
        {
            //get all the selected checkbox and delete the records

            string idsToDelete = "";

            CheckBox ChkBoxHeader = (CheckBox)grdRoles.HeaderRow.FindControl("cbSelectAll");

            foreach (GridViewRow row in grdRoles.Rows)
            {
                // Only look in data rows, ignore header and footer rows
                if (row.RowType == DataControlRowType.DataRow)
                {
                    CheckBox ChkBoxRows = (CheckBox)row.FindControl("cbSelect");

                    if (ChkBoxHeader.Checked == true)
                    {
                        ChkBoxRows.Checked = true;
                        var id = grdRoles.DataKeys[row.RowIndex].Value;
                        idsToDelete = idsToDelete + "," + id;

                    }
                    //else
                    //{
                    //    ChkBoxRows.Checked = false;
                    //}
                }
            }

            if (idsToDelete != "")
            {
                BLL.Role BLL_objRole = new BLL.Role();
                BLL_objRole.DeleteMultiple(idsToDelete.Substring(1,idsToDelete.Length));
                //divAlert.Visible = true;
                

                //ClearFields();
                BindGrid();
            }

           
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            //divAlert.Visible = false;
            //divAlertDelete.Visible = false;

            //BO.Role BO_objRole = new BO.Role();
            //BO_objRole.ID = Convert.ToInt32(hdnID.Value.Trim());
            //BO_objRole.RoleName = txtRoleName.Text.Trim();
            //BO_objRole.Description = txtDescription.Text.Trim();
            //BLL.Role BLL_objRole = new BLL.Role();
            //BLL_objRole.Save(BO_objRole);
            //divAlert.Visible = true;
            //if (BO_objRole.ID == 0)
            //{
            //    ltrMessage.Text = "This record has been saved successfully.";
            //}
            //else
            //{
            //    ltrMessage.Text = "This record has been updated successfully.";
            //}

            //ClearFields();
            BindGrid();
        }
        #endregion        

        #region GridView and Method

        private void MakeAccessible(GridView grid)
        {
            if (grid.Rows.Count > 0)
            {
                //This replaces <td> with <th> and adds the scope attribute
                grid.UseAccessibleHeader = true;
                //grid.FooterRow.TableSection = TableRowSection.TableFooter;

                //This will add the <thead> and <tbody> elements
                grid.HeaderRow.TableSection = TableRowSection.TableHeader;

                //This adds the <tfoot> element. Remove if you don't have a footer row
                //grid.FooterRow.TableSection = TableRowSection.TableFooter;
            }
        }

        public void BindGrid()
        {
            BLL.Role objRole = new BLL.Role();
            DataTable dt = objRole.GetAllRoles();
            grdRoles.DataSource = dt;
            grdRoles.DataBind();
            MakeAccessible(grdRoles);
        }

        protected void grdRoles_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            int intRoleID = Convert.ToInt32(e.CommandArgument);
            GridViewRow row = (GridViewRow)(((Button)e.CommandSource).NamingContainer);

            if (e.CommandName == "EditCommand")
            {
               Response.Redirect("AddRoles.aspx?RoleID="+intRoleID); 
                //TextBox textBox = (TextBox)row.FindControl("TextBoxAddPost");

                //Label lblName = (Label)row.FindControl("lblName") as Label;

                //txtRoleName.Text = row.Cells[0].Text;
                ////Label lblDescription = (Label)row.FindControl("lblDescription") as Label;
                //txtDescription.Text = row.Cells[1].Text;
                //hdnID.Value = intRoleID.ToString();
            }
            else if (e.CommandName == "EditCommand")
            {
                BLL.Role BLL_objRole = new BLL.Role();

                //get the status of deleted item
                int IsDeleted = BLL_objRole.Delete(intRoleID);


                if (IsDeleted == 0)
                {
                    divAlertDelete.Visible = true;
                    ltrMessageDelete.Text = "This record has been deleted successfully.";
                    BindGrid();
                }
                else
                {
                    //show data exist.
                    divAlertDelete.Visible = true;
                    ltrMessageDelete.Text = "Cannot delte this record. Data exist under this record.";

                }
            }
            else
            {
            }

        }

        #endregion

    }
}